package com.service.imp;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.dao.DeptDao;
import com.dao.MeasureDao;
import com.dao.StaffDao;
import com.dao.UserDao;
import com.dao.UserSettingDao;
import com.model.DeptBean;
import com.model.MeasureBean;
import com.model.StaffBean;
import com.model.UserBean;
import com.model.UserSettingBean;

@Component(value = "testService")
@Transactional(rollbackFor = {Exception.class }, propagation = Propagation.REQUIRED)
public class TestService {
	@Autowired
	private UserDao userDao;
	@Autowired
	private StaffDao staffDao;
	@Autowired
	private DeptDao deptDao;
	@Autowired
	private MeasureDao measureDao;
	@Autowired
	private UserSettingDao userSettingDao;
	public boolean save() {
		boolean is = false;
		
			UserBean userBean = new UserBean();
			userBean.setUser_account("声明试事务");
			userBean.setUser_pass("123");
			userDao.save(userBean);
			@SuppressWarnings("unused")
			int i = 10 / 0;
			is=true;
			return is;
			
	}
	//public StaffBean  saveStaff(int count){
	public void  saveStaff(int count){
		String[] name={"语蝶","依波","晓旋","念之","盼芙","曼安",
					"采珊","盼夏","初柳","迎天","曼安","南珍",
					"妙芙","语柳","含莲","晓筠","夏山","尔容",
					"采春","念梦","傲南","问薇","雨灵","凝安",
					"冰海","初珍","宛菡","冬卉","盼晴","冷荷",
					"寄翠","幻梅","如凡","语梦","易梦","千柔",
					"向露","梦玉","傲霜","依霜","灵松","诗桃",
					"书蝶","恨真","冰蝶","山槐","以晴","友易",
					"梦桃","香菱","孤云","水蓉","雅容","飞烟",
					"雁荷","代芙","醉易","夏烟","山梅","若南",
					"恨桃","依秋","依波","香巧","紫萱","涵易",
					"忆之","幻巧","水风","安寒","白亦","惜玉","碧春","怜雪"};
		List<String> list=Arrays.asList(name);
		//String[] dept={"总经办","行政部","人事部","财务部","上海市","广州市","保卫部","工程部"};
		String[] sex={"男","女"};
		Integer[] deptid={10,20,30,40,60,70,5001,5002,5003};
		String[] address ={"鼓楼区杨桥西路北侧市国家安全局东侧","福州市鼓楼区通湖路224号","福建省福州市东街134号",
							"台江区交通路88号","广达路73号","鼓楼区乌山路53号",
							"工业路168号(洋头口西侧)","交通路医科大学往宝龙方向","广达路368号",
							"白马南路10号（原玻璃厂、保温瓶厂）","福州市台江区交通路1号","广达路272号附近",
							"台江区白马路与工业路交叉口","上海街道西洋路45号","福州市台江区西洋路163号",
							"鼓楼区乌山西路69号","台江区六一中路东侧","西二环路万象城对面",
							"鼓楼区安泰街道新权南路36号","福州市鼓楼区灵响路63号","红庆里19",
							"工业路252号","群众路53号附近","福屿路281号"}; 
		String[] eb={"博士","硕士","本科","专科","高中","初中"};
		Random random = new Random();
		
		StaffBean sb= new StaffBean();
		DeptBean db=this.deptDao.get(deptid[Math.abs(random.nextInt())%deptid.length]);
		sb.setDept_id(db);
		
		sb.setStaff_address(address[Math.abs(random.nextInt()%address.length)]);
		
		SimpleDateFormat df= new SimpleDateFormat("yyyy-MM-dd");
		Date randomDate = randomDate("1980-01-01", "2000-03-01");
		sb.setStaff_date_birth(df.format(randomDate));
		
		sb.setStaff_eb(eb[Math.abs(random.nextInt()%eb.length)]);
		
		randomDate = randomDate("2015-01-01", "2015-04-01");
		sb.setStaff_hire(df.format(randomDate));
		
		sb.setStaff_id_card(random.nextLong()+"");
		int i=Math.abs(random.nextInt())%list.size();
		String s=list.get(i);
		sb.setStaff_name(s);
		
		
		DecimalFormat df1=new DecimalFormat("00000");
	    String str2=df1.format(count);
		sb.setStaff_number("KTB"+str2);
		
		sb.setStaff_phone((long)Math.floor((Math.random()+1)*10000000000l)+"");
		//df1=new DecimalFormat("#.00"); 保留2位小数
		
		sb.setStaff_salary(Math.floor(Math.random()*10000));
		sb.setStaff_sex(sex[Math.abs(random.nextInt()%sex.length)]);
		sb.setIs_turnover(false);
		this.staffDao.save(sb);
		//return sb;
	}
	public void updtauser_setting(){
		List<UserBean> list=this.userDao.loadAll();
		for(UserBean user:list){
			UserSettingBean utb = new UserSettingBean();
			utb.setSetting_key("tb_assets");
			utb.setSetting_value("a_number,a_class_name,a_specification,a_name,a_measure,a_source,a_status,a_custody_staff,a_store,a_unit_price,a_years,a_jc_rate,a_d_m,a_ytd,a_count_d,a_j_value,a_buy_date,a_register_data,a_f_number,a_remark,a_machine_no");
			utb.setUser_account(user);
			this.userSettingDao.save(utb);
			
		}
	}
	@SuppressWarnings("resource")
	public static void main(String[] args) throws Exception {


//		 Date randomDate = randomDate("1995-01-01", "2015-03-01");  
//			SimpleDateFormat df= new SimpleDateFormat("yyyy-MM-dd");
//	        System.out.println(df.format(randomDate));  

		ApplicationContext context = new ClassPathXmlApplicationContext(
				"applicationContext.xml");
		TestService testService = (TestService) context.getBean("testService");
		try {
//			for(int i=0;i<1000;i++){
//				testService.saveStaff(i);
//			}
			//testService.savetest();
			testService.updtauser_setting();
			DecimalFormat df1=new DecimalFormat("00000");
			System.out.println(df1.format(1));
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			System.out.println(11111111);
			/**
			 * System.out.println("id:"+s.getId());
				System.out.println("地址:"+s.getStaff_address());
				System.out.println("生日:"+s.getStaff_date_birth());
				System.out.println("学历:"+s.getStaff_eb());
				System.out.println("入职时间:"+s.getStaff_hire());
				System.out.println("身份证:"+s.getStaff_id_card());
				System.out.println("姓名:"+s.getStaff_name());
				System.out.println("编号:"+s.getStaff_number());
				System.out.println("手机:"+s.getStaff_phone());
				System.out.println("性别:"+s.getStaff_sex());
				System.out.println("薪水:"+s.getStaff_salary());
				System.out.println("部门"+s.getDept_id().getId());
			 */
		}
		

	}

  
    /** 
     * 获取随机日期 
     *  
     * @param beginDate 
     *            起始日期，格式为：yyyy-MM-dd 
     * @param endDate 
     *            结束日期，格式为：yyyy-MM-dd 
     * @return 
     */  
  
    private static Date randomDate(String beginDate, String endDate) {  
        try {  
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");  
            Date start = format.parse(beginDate);// 构造开始日期  
            Date end = format.parse(endDate);// 构造结束日期  
            // getTime()表示返回自 1970 年 1 月 1 日 00:00:00 GMT 以来此 Date 对象表示的毫秒数。  
            if (start.getTime() >= end.getTime()) {  
                return null;  
            }  
            long date = random(start.getTime(), end.getTime());  
  
            return new Date(date);  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        return null;  
    } 
    public void savetest(){
    	MeasureBean mb= new MeasureBean();
    	for(int i=0;i<10;i++){
    		mb.setMeasure_id(null);
    		System.out.println(mb.getMeasure_id());
	    	mb.setMeasure_name("cccdd"+i);
	    	mb.setMeasure_type("cc");
	    	this.s(mb);
	    	System.out.println(mb.getMeasure_id());
    	}
    }
    public void s(MeasureBean m){
    	MeasureBean mesBean=m;
    	this.measureDao.save(mesBean);
    }
    private static long random(long begin, long end) {  
        long rtn = begin + (long) (Math.random() * (end - begin));  
        // 如果返回的是开始时间和结束时间，则递归调用本函数查找随机值  
        if (rtn == begin || rtn == end) {  
            return random(begin, end);  
        }  
        return rtn;  
    }
//	StringBuffer sb=new StringBuffer();
//	sb.append("insert into tb_staff a (a.id,");
//	sb.append("a.staff_number,");
//	sb.append("a.staff_name,a.staff_sex,a.staff_eb,");
//	sb.append("a.staff_phone,a.staff_address,a.staff_post,");
//	sb.append("a.staff_id_card,a.staff_hire,a.staff_date_birth,");
//	sb.append("a.staff_salary) values");
}
